﻿using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PublicDomain.Framework.UnitTesting.DataDrivenTest
{
    public class DataDrivenTest<TTestParams, TExpectedResults>
        where TTestParams : new()
        where TExpectedResults : new()
    {
        public DataDrivenTest(TestContext testContext)
        {
            this.TestContext = testContext;

            this.TestInfo = testContext.DataRow.ReadObject<DataDrivenTestInfo>();
            this.TestParams = testContext.DataRow.ReadObject<TTestParams>();
            this.ExpectedResults = testContext.DataRow.ReadObject<TExpectedResults>();
        }

        public TestContext TestContext { get; private set; }

        public DataDrivenTestInfo TestInfo { get; private set; }
        
        public TTestParams TestParams { get; private set; }

        public TExpectedResults ExpectedResults { get; private set; }

        public void ExecuteTest(Action testAction)
        {
            Console.WriteLine(this.TestInfo.ToConsoleText());

            if (this.TestInfo.Ignore)
            {
                Console.WriteLine("The test has been skipped.");

                return;
            }

            testAction();
        }
    }
}
